package com.baize.baizeapp.test.video

import android.content.Context
import android.graphics.Bitmap
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import com.baize.baizeapp.R
import com.bumptech.glide.Glide


class HSPreVideoView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleRes: Int = 0
) : FrameLayout(context, attrs, defStyleRes) {

    var ivCover : ImageView
    var videoUrl = ""
    var coverUrl = ""
    var coverBitmap : Bitmap? = null

    init {
        val view: View = LayoutInflater.from(getContext()).inflate(
            R.layout.video_pre_view, this, true
        )
        ivCover = view.findViewById<ImageView>(R.id.iv_cover)
    }

    fun initVideo(vdieoUrl: String, coverUrl: String) {
        this.videoUrl = vdieoUrl
        this.coverUrl = coverUrl
        showThumb()
    }

    fun showThumb() {
        if (TextUtils.isEmpty(coverUrl)) return
        Glide.with(this)
            .load(coverBitmap ?: coverUrl)
            .into(ivCover)
        ivCover.visibility = View.VISIBLE
    }

    fun hideConer() {
        ivCover.visibility = View.GONE
    }

    fun attachVideo(videoView:HeiShiTTVideo) {
        addView(videoView,0)
        hideConer()
    }

    fun removeVideo(videoView: HeiShiTTVideo) {
        coverBitmap = videoView.textureView.getBitmap()
        removeView(videoView)
        showThumb()
    }

//    fun attachVideoView(videoView: HeiShiTTVideo) {
//        mVideoView = videoView
//        mVideoView?.addView(videoView)
//        mVideoView?.setUrl(videoUrl)
//        mVideoView?.play()
//    }

}


















